package top.IT00544.utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * JDBC 的工具类
 * 
 * 其中包含: 获取数据库连接, 关闭数据库资源等方法.
 */
public class JDBC2Tools {
	public static Connection connection = null;
	
	public static Connection getConnection() throws Exception {
		if(null!=connection) {
			return connection;
		}
		// 1. 准备获取连接的 4 个字符串: user, password, jdbcUrl, driverClass
		String user = "root";
		String password = "kongkong";
		String jdbcUrl = "jdbc:mysql://localhost:3306/novel_v1?useUnicode=true&characterEncoding=UTF-8";
		String driverClass = "com.mysql.jdbc.Driver";
//		Properties properties = new Properties();
//		InputStream inStream = JDBC2Tools.class.getClassLoader().getResourceAsStream("jdbc.properties");
//		properties.load(inStream);
//		String user = properties.getProperty("user");
//		String password = properties.getProperty("password");
//		String jdbcUrl = properties.getProperty("jdbcUrl");
//		String driverClass = properties.getProperty("driverClass");
		// 2. 加载驱动: Class.forName(driverClass)
		Class.forName(driverClass);

		// 3. 调用
		// DriverManager.getConnection(jdbcUrl, user, password)
		// 获取数据库连接
		connection = DriverManager.getConnection(jdbcUrl, user, password);
		return connection;
	}

	public static void releaseDB(ResultSet resultSet, Statement statement,
			Connection connection) {

		if (resultSet != null) {
			try {
				resultSet.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}

		if (statement != null) {
			try {
				statement.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}

		if (connection != null) {
			try {
				connection.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

}
